#version 330 core

#define MAX 16

layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec2 vertexUV;
layout(location = 2) in vec3 vertexNormal_modelspace;
layout(location = 3) in vec3 vertexTangent_modelspace;
layout(location = 4) in vec3 vertexBitangent_modelspace;

uniform mat4 MVP;
uniform mat4 V;
uniform mat4 M;
uniform mat4 P;
uniform vec3 LightPosition[MAX];
uniform int NoLights;

out vec2 UV;
out vec3 Position_worldspace;
out vec3 EyeDirection_cameraspace;
out vec3 LightDirection_cameraspace[MAX];


out vec3 o_vertexNormal_modelspace;
out vec3 o_vertexTangent_modelspace;
out vec3 o_vertexBitangent_modelspace;

void main(){

	vec3 vertexPosition_cameraspace = ( V * M * vec4(vertexPosition_modelspace,1)).xyz;
	EyeDirection_cameraspace = vec3(0,0,0) - vertexPosition_cameraspace;


	gl_Position =  P * V * M * vec4(vertexPosition_modelspace,1);
	Position_worldspace = (M * vec4(vertexPosition_modelspace,1)).xyz;
	
	for(int i = 0; i < NoLights; i++)
	{
		
		vec3 LightPosition_cameraspace = ( V * vec4(LightPosition[i],1)).xyz;
		LightDirection_cameraspace[i] = LightPosition_cameraspace + EyeDirection_cameraspace;
		
		//	Normal_cameraspace = ( V * M * vec4(vertexNormal_modelspace,0)).xyz; // Only correct if ModelMatrix does not scale the model ! Use its inverse transpose if not.
	}

	UV = vertexUV;
	
	o_vertexNormal_modelspace = normalize((  M * vec4(vertexNormal_modelspace,0)).xyz);
	o_vertexTangent_modelspace = normalize((  M * vec4(vertexTangent_modelspace,0)).xyz); // normalize(vertexTangent_modelspace);
	o_vertexBitangent_modelspace = normalize(( M * vec4(vertexBitangent_modelspace,0)).xyz); //normalize(vertexBitangent_modelspace);

}